<?php
namespace  app\admin\model;

use think\Exception;
use think\facade\Db;
use think\facade\Request;
use think\Model;
use think\model\concern\SoftDelete;

//属性页
class GoodsProp extends Model{
    protected $pk='pid';
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    //prop 属性列表sql语句
    public static function  propList($where,$whereOr){
        $prop=GoodsProp::
            field(true)
            ->where($where)
            ->where(function ($query) use ($whereOr){
                $query->whereOr($whereOr);
            })
            ->select();
        return $prop;
    }
    //prop属性值查询的sql语句
     public static function   propValue(){
         $prop_value=GoodsProp::suffix('_value')->field('vid,prop_value')->select()->toArray();
         return $prop_value;
         }
         //获取器 效果转中文输出
    public static function   getXiaoguoTextAttr($v,$data){
        if ($data['xiaoguo']==1){
            return '<font color="#00CC99">有效</font>';
        }else{
            return '<font color="#CE3E3E">无效</font>';
        }
    }
        //prop属性页面加新属性值
     public static function   goodPropValue($prop_value){
           // 启动事务
            Db::startTrans();
        try{
            foreach ($prop_value as $v){
                $prop_values[] = [
                    'prop_value' => $v
                ];
            }
            foreach ($prop_values as $v){
                $pvlues[]=GoodsProp::suffix('_value')->insertGetId($v);
            }
            // 提交事务
            Db::commit();
        }catch (Exception $e){
            // 回滚事务
            Db::rollback();
        }
        return $pvlues;
     }
    // prop属性添加
     public static function   goodPropAddInfo($post){
        try{
            $res=GoodsProp::insert($post);
        }catch (Exception $e){
            $res=false;
            echo $e->getMessage();
        }
            return $res;
     }

    // prop属性删除
      public static function    goodPropDelInfo($get){
        try{
            $res=GoodsProp::destroy($get['id']);
        }catch (Exception $e){
            $res=false;
        }
            return $res;

      }

      //prop 属性修改
     public static function goodPropEditInfo($param){
        if (Request::isPost()){
            try{
                $res=GoodsProp::update($param);
            }catch (Exception $e){
                $res=false;
            }
            return $res;

        }else{
            $find=GoodsProp::find($param['pid']);
            $find['pvalues']=explode(',',$find['pvalues']);
            return $find;
        }

     }


}